package com.wenan.ui.controls.command
{
	import com.wenan.AppGlobal;
	import com.wenan.data.PageInfo;
	import com.wenan.ui.controls.renderer.NavigatorMenuRenderer;
	import com.wenan.ui.controls.standard.LinkLabel;
	
	import flash.events.MouseEvent;
	
	import mx.containers.HBox;
	import mx.controls.Image;
	
	import spark.components.Label;
	
	public class NavigatorControl extends HBox
	{
		private var _pageInfos: Array = [];
		
		public function NavigatorControl()
		{
			super();
			
			setStyle("horizontalGap", "6");
		}

		public function get pageInfos():Array
		{
			return _pageInfos;
		}

		public function set pageInfos(value:Array):void
		{
			removeAllElements();
			
			_pageInfos = value;
			
			buildElements();
		}
		
		private function buildElements():void
		{
			addNavigatorLabel("导航栏", pageInfo, "#44a9bf",false , i == _pageInfos.length-1);	
			
			if (_pageInfos)
			{
				var pageInfo:PageInfo;
				for(var i:int=0;i<_pageInfos.length;i++)
				{
					pageInfo = _pageInfos[i];
					
					if (i<_pageInfos.length-1)
					{
						addLinkIcon("assets/navigatormenu/icon.png", 0.5);	
					}
					else
					{
						addLinkIcon("assets/navigatormenu/last_icon.png", 1.0);	
					}

					addNavigatorLabel(pageInfo.parameters.findByName("navName").value,pageInfo, "#084b59",true, i == _pageInfos.length-1);	
				}
			}
		}
		
		private function addLinkIcon(source:Object, alpha:Number):Image
		{
			var image:Image = new Image();
			image.source = source;
			image.top = 7;
			image.includeInLayout = true;
			image.alpha = alpha;
			addElement(image);
			return image;
		}
		
		private function addNavigatorLabel(text:String, pageInfo:PageInfo, color:String, linkable, lastLabel:Boolean):LinkLabel
		{
			var label:LinkLabel = new LinkLabel();
			label.text = text;
			label.linkable = linkable;
			label.defaultColor = color;
			label.data = pageInfo;
//			label.customColor = "#1f4580";
//			label.hoverColor = "#1f4580"; 
			if (lastLabel)
			{
				label.alpha = 1.0;
			}
			else
			{
				label.alpha = 1.0;
			}
			label.addEventListener(MouseEvent.CLICK, clickHandler);
			addElement(label);
			return label;
		}
		
		private function clickHandler(event:MouseEvent):void
		{
			if (AppGlobal.modulePageContainer)
			{
				var label:LinkLabel = event.target as LinkLabel;
				if(label.data)
				{
					var pageInfo:PageInfo = label.data as PageInfo;	
					AppGlobal.modulePageContainer.navigate(pageInfo,false);
				}
			}			
		}
		
	}
}